package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    //根据动态id和类型，获取互动信息
    @Override
    public List<Comment> findComments(String movementId, Integer commentType, Integer page, Integer pageSize) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(commentType);
        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((page - 1) * pageSize)
                .limit(pageSize);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }

    //保存互动数据，返回互动总数
    @Override
    public Integer save(Comment comment) {
        //0. 获取互动类型
        Integer commentType = comment.getCommentType();
        //1. 获取动态id
        ObjectId movementId = comment.getPublishId();
        //2. 根据动态id，获取动态发布人对象
        Movement movement = mongoTemplate.findById(movementId, Movement.class);
        //3. 设置动态发布人id到publishUserId
        comment.setPublishUserId(movement.getUserId());
        //4. 保存
        mongoTemplate.save(comment);
        //5. 更新动态，获取最新数据
        //5.1 条件
        Criteria criteria = Criteria.where("id").is(movementId);
        Query query = Query.query(criteria);
        //5.2 更新字段
        Update update = new Update();
        if (commentType == 1) {
            update.inc("likeCount", 1);
        } else if (commentType == 2) {
            update.inc("commentCount", 1);
        } else {
            update.inc("loveCount", 1);
        }
        //5.3 更新配置
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        if (commentType == 1) {
            return modify.getLikeCount();
        } else if (commentType == 2) {
            return modify.getCommentCount();
        } else {
            return modify.getLoveCount();
        }
    }

    @Override
    public Integer saveVideo(Comment comment) {
        //0. 获取互动类型
        Integer commentType = comment.getCommentType();
        //1. 获取动态id
        ObjectId movementId = comment.getPublishId();
        //2. 根据动态id，获取动态发布人对象
        Video video = mongoTemplate.findById(movementId, Video.class);
        //3. 设置动态发布人id到publishUserId
        comment.setPublishUserId(video.getUserId());
        //4. 保存
        mongoTemplate.save(comment);
        //5. 更新动态，获取最新数据
        //5.1 条件
        Criteria criteria = Criteria.where("id").is(movementId);
        Query query = Query.query(criteria);
        //5.2 更新字段
        Update update = new Update();
        if (commentType == 1) {
            update.inc("likeCount", 1);
        } else if (commentType == 2) {
            update.inc("commentCount", 1);
        } else {
            update.inc("loveCount", 1);
        }
        //5.3 更新配置
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Video video1 = mongoTemplate.findAndModify(query, update, options, Video.class);
        if (commentType == 1) {
            return video1.getLikeCount();
        } else if (commentType == 2) {
            return video1.getCommentCount();
        } else {
            return video1.getLoveCount();
        }
    }


    //取消点赞
    @Override
    public Integer delete(Integer commentType, Long userId, String id) {
        //1. 获取参数  互动类型，互动人，动态id
        //2. 先删除互动数据
        Criteria criteria = Criteria.where("publishId").is(id)
                .and("commentType").is(commentType)
                .and("userId").is(userId);
        Query query = Query.query(criteria);
        mongoTemplate.remove(query, Comment.class);
        //3. 更新动态表中的数据，获取最新值
        Criteria movementCriteria = Criteria.where("id").is(new ObjectId(id));
        Query movementQuery = Query.query(movementCriteria);
        Update update = new Update();
        if (commentType == 1) {
            update.inc("likeCount", -1);
        } else if (commentType == 2) {
            update.inc("commentCount", -1);
        } else {
            update.inc("loveCount", -1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement movement = mongoTemplate.findAndModify(movementQuery, update, options, Movement.class);
        if (commentType == 1) {
            return movement.getLikeCount();
        } else if (commentType == 2) {
            return movement.getCommentCount();
        } else {
            return movement.getLoveCount();
        }
    }

    //取消视频点赞
    @Override
    public Integer deleteByvideo(Integer commentType, Long userId, String id) {
        //1. 获取参数  互动类型，互动人，动态id
        //2. 先删除互动数据
        Criteria criteria = Criteria.where("publishId").is(id)
                .and("commentType").is(commentType)
                .and("userId").is(userId);
        Query query = Query.query(criteria);
        mongoTemplate.remove(query, Comment.class);
        //3. 更新动态表中的数据，获取最新值
        Criteria movementCriteria = Criteria.where("id").is(new ObjectId(id));
        Query movementQuery = Query.query(movementCriteria);
        Update update = new Update();
        if (commentType == 1) {
            update.inc("likeCount", -1);
        } else if (commentType == 2) {
            update.inc("commentCount", -1);
        } else {
            update.inc("loveCount", -1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Video video = mongoTemplate.findAndModify(movementQuery, update, options, Video.class);
        if (commentType == 1) {
            return video.getLikeCount();
        } else if (commentType == 2) {
            return video.getCommentCount();
        } else {
            return video.getLoveCount();
        }
    }

    //返回评论数量
    @Override
    public Long findCount(String id, Integer commentType) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(id)).and("commentType").is(commentType);
        Query query = Query.query(criteria);
        long count = mongoTemplate.count(query, Comment.class);
        return count;
    }
}
